
function Calendario(param, dia, mes, anho, selected) {
    this.param = param
    this.dia = parseFloat(dia)
    this.mes = parseFloat(mes - 1)
    this.mes = this.mes % 12;
    this.anho = parseFloat(anho)
    var b4 = ((anho % 4) == 0)
    var b100 = ((anho % 100) == 0)
    var b400 = ((anho % 400) == 0)

    this.bisiesto = b4 && !b100 || b4 && b400
    this.selected = selected
    this.editable = arguments[5];
    this.disabled = arguments[6];
    this.callback = arguments[7];

    Calendario.prototype.contador++
    this.id = 'calendario' + Calendario.prototype.contador;
    this.objetos[this.id] = this
}

Calendario.prototype.write = function() {
    if (!isNaN(arguments[0]) && !isNaN(arguments[1]) && !isNaN(arguments[2])) {

        this.dia = parseFloat(arguments[0])
        this.mes = parseFloat(arguments[1] - 1)
        this.mes = this.mes % 12;
        this.anho = parseFloat(arguments[2])
        var b4 = ((arguments[2] % 4) == 0)
        var b100 = ((arguments[2] % 100) == 0)
        var b400 = ((arguments[2] % 400) == 0)

        this.bisiesto = b4 && !b100 || b4 && b400
    }

    var dia = this.dia;
    if (dia < 10)
        dia = '0' + dia;

    var mes = this.mes + 1;
    if (mes < 10)
        mes = '0' + mes;

    document.write('<input class="input_calendario" ' + (this.editable ? ' onblur="Calendario.prototype.objetos[\'' + this.id + '\'].parsear(this)"' : 'onfocus="this.blur()"') + ' id="' + this.param + '" name="' + this.param + '" type="text" size="10"');
    if (this.selected)
        document.write(' value="' + dia + '/' + mes + '/' + this.anho + '"');
    if (this.disabled)
        document.write(' disabled="disabled"');
    if (this.callback)
        document.write(' onkeyup="' + this.callback + '(this.name,this.value,true)"')
    document.write(' />');
    document.write('<a class="boton_calendario" onfocus="this.blur()" href="#" onclick="Calendario.prototype.objetos[\'' + this.id + '\'].interruptor();return false"><img src="' + this.imgBoton + '" /></a>');
    document.write('<div id="' + this.id + '" style="display:inline;position:relative">');
    document.write('<div class="calendario" id="' + this.id + '_mes" style="position:absolute;display:none;">&nbsp;</div>');
    document.write('</div>');
}
Calendario.prototype.parsear = function(input) {
    if (input.value == '')
        return;

    var partes = input.value.split('/');

    if (partes.length != 3) {
        if (input.defaultValue != null && input.defaultValue != '')
            input.value = input.defaultValue
        else {
            input.value = '';
            return
        }
    }
    partes = input.value.split('/');

    var p;

    p = parseFloat(partes[2])

    if (partes[2] == null || isNaN(partes[2] || isNaN(p)))
        partes[2] = '1950'

    else if (p == 0)
        partes[2] = '1950'

    else
        partes[2] = p.toString();

    while (partes[2].length < 4)
        partes[2] = '0' + partes[2]

    this.anho = parseFloat(partes[2]);
    var b4 = ((this.anho % 4) == 0)
    var b100 = ((this.anho % 100) == 0)
    var b400 = ((this.anho % 400) == 0)
    this.bisiesto = (b4 && !b100 || b4 && b400)

    p = parseFloat(partes[1])

    if (partes[1] == null || isNaN(partes[1]) || isNaN(p))
        partes[1] = '01'
    else if (p == 0)
        partes[1] = '01'
    else
        partes[1] = p.toString();

    while (partes[1].length < 2)
        partes[1] = '0' + partes[1]

    this.mes = parseFloat(partes[1]) - 1

    p = parseFloat(partes[0])
    if (partes[0] == null || isNaN(partes[0]) || isNaN(p))
        partes[0] = '01'
    else if (p == 0)
        partes[0] = '01'
    else
        partes[0] = p.toString();

    while (partes[0].length < 2)
        partes[0] = '0' + partes[0]

    this.dia = parseFloat(partes[0])
    if (this.dia > this.meses[this.mes] + 28) {
        this.dia = this.meses[this.mes] + 28

        if (this.mes == 1 && this.bisiesto)
            this.dia++
    }

    input.value = partes.join('/')
    if (this.callback != null)
        eval(this.callback + '(\'' + this.param + '\',\'' + this.dia + '/' + (this.mes + 1) + '/' + this.anho + '\',true)')

    this.dibujarMes();
}
Calendario.prototype.interruptor = function() {
    var input = document.getElementById(this.param)

    if (!input.disabled) {

        var mes = document.getElementById(this.id + '_mes');
        if (mes.style.display == 'none') {
            this.dibujarMes();
            mes.style.display = 'block';
        }
        else
            mes.style.display = 'none';
    }
}

Calendario.prototype.cambiarDia = function(dia) {
    this.dia = dia
    if (dia < 10)
        dia = '0' + dia

    mes = this.mes + 1
    if (mes < 10)
        mes = '0' + mes

    document.getElementById(this.param).value = dia + '/' + mes + '/' + this.anho

    if (this.callback != null)
        eval(this.callback + '(\'' + this.param + '\',\'' + dia + '/' + mes + '/' + this.anho + '\',true)')

    this.dibujarMes();
    this.interruptor();
}

Calendario.prototype.cambiarMes = function(dir) {
    this.mes = dir;

    if (this.dia > this.meses[this.mes] + 28) {
        this.dia = this.meses[this.mes] + 28

        if (this.mes == 1 && this.bisiesto)
            this.dia++
    }

    this.dibujarMes();
}

Calendario.prototype.cambiarAnho = function(dir) {
    this.anho = parseInt(this.anho) + parseInt(dir);
    var b4 = ((this.anho % 4) == 0)
    var b100 = ((this.anho % 100) == 0)
    var b400 = ((this.anho % 400) == 0)

    this.bisiesto = (b4 && !b100 || b4 && b400)

    if (this.anho < 1590) {
        this.anho = 1590
        this.bisiesto = false;
    }
    else {
        if (this.mes == 1) {
            if (this.bisiesto) {
                if (this.dia > 29)
                    this.dia = 29
            }
            else {
                if (this.dia > 28)
                    this.dia = 28
            }
        }

        this.dibujarMes();
    }
}

Calendario.prototype.dibujarMes = function() {

    //calculamos el numero de dias que han pasado desde el 1 de Enero de 1990 que fue lunes
    //en teor�a en 1590 tb fue lunes el 1 de enero
    //el cambio de calendario se produce en 1582 (en espa�a)
    var dias = 0;
    var b4;
    var b100
    var b400

    //tengo que tener en cuenta el a�o secular (aquellos que terminan en 00) que
    //contamos los dias que han pasado en los a�os anteriores desde 1990
    for (var i = 1590; i < this.anho; i++) {
        b4 = ((i % 4) == 0)
        b100 = ((i % 100) == 0)
        b400 = ((i % 400) == 0)

        if (b4 && !b100 || b4 && b400)
            dias += 366
        else
            dias += 365
    }

    //contamos los dias que han pasado en los meses anteriores de este anho
    for (var i = 0; i < this.mes; i++) {
        dias += this.meses[i] + 28
        if (i == 1 && this.bisiesto) {
            dias++
        }
    }

    var desplazamientoDia1 = dias % 7; //empieza en 0 y acaba en 6

    var maxDias = this.meses[this.mes] + 28;
    if (this.mes == 1 && this.bisiesto)
        maxDias++;

    var numSemanas = Math.ceil((desplazamientoDia1 + maxDias) / 7)


    var html = '';
    html += '<p class="cal_anho"><a href="#" onclick="Calendario.prototype.objetos[\'' + this.id + '\'].cambiarAnho(-1);return false">-</a>' + this.anho + '<a href="#" onclick="Calendario.prototype.objetos[\'' + this.id + '\'].cambiarAnho(1);return false">+</a></p>'
    html += '<p class="cal_mes"><select name="cal_mes_' + this.id + '" onchange="Calendario.prototype.objetos[\'' + this.id + '\'].cambiarMes(this.selectedIndex)">'
    for (var i = 0; i < this.nombreMeses.length; i++)
        html += '<option value="' + i + '" ' + ((this.mes == i) ? 'selected="selected"' : '') + '>' + this.nombreMeses[i] + '</option>'
    html += '</select></p>'

    html += '<table width="100%" cellpadding="0" cellspancing="0" class="cal_tabla"><thead>'
    html += '<tr><th class="dia_semana">' + this.nombreDiasSemana[0] + '</th><th class="dia_semana">' + this.nombreDiasSemana[1] + '</th><th class="dia_semana">' + this.nombreDiasSemana[2] + '</th><th class="dia_semana">' + this.nombreDiasSemana[3] + '</th><th class="dia_semana">' + this.nombreDiasSemana[4] + '</th><th class="dia_finde">' + this.nombreDiasSemana[5] + '</th><th class="dia_finde">' + this.nombreDiasSemana[6] + '</th></tr>'
    html += '</thead><tbody>'

    var casilla = 1;
    for (i = 0; i < numSemanas; i++) {
        html += '<tr>'

        for (var j = 0; j < 7; j++) {

            html += '<td'
            if (j == 5 || j == 6)
                html += ' class="dia_finde" onmouseover="this.className=\'dia_finde_over\'"  onmouseout="this.className=\'dia_finde\'"'
            else
                html += ' class="dia_semana" onmouseover="this.className=\'dia_semana_over\'"  onmouseout="this.className=\'dia_semana\'"'
            html += '>'

            if ((casilla <= desplazamientoDia1) || (casilla > desplazamientoDia1 + maxDias)) {
                html += '&nbsp;';
            }
            else {
                if (casilla - desplazamientoDia1 == this.dia)
                    html += '<strong>'
                html += '<a href="#" onclick="Calendario.prototype.objetos[\'' + this.id + '\'].cambiarDia(' + (casilla - desplazamientoDia1) + ');return false">' + (casilla - desplazamientoDia1) + '</a>'
                if (casilla - desplazamientoDia1 == this.dia)
                    html += '</strong>'
            }

            html += '</td>'
            casilla++
        }

        html += '</tr>'
    }

    html += '</tbody></table>'

    document.getElementById(this.id + '_mes').innerHTML = html

}

Calendario.prototype.objetos = new Object();
Calendario.prototype.contador = 0;
Calendario.prototype.meses = new Array(3, 0, 3, 2, 3, 2, 3, 3, 2, 3, 2, 3);
Calendario.prototype.nombreMeses = new Array('Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre');
Calendario.prototype.nombreDiasSemana = new Array('L', 'M', 'M', 'J', 'V', 'S', 'D');

Calendario.prototype.imgBoton = 'imagenes/calendar.gif';

/*
 Calendario.prototype.imgFlechaIzqMeses='imagenes/cal_flecha_izq.gif'
 Calendario.prototype.imgFlechaDerMeses='imagenes/cal_flecha_der.gif'
 Calendario.prototype.imgFlechaIzqAnhos='imagenes/cal_flecha_izq.gif'
 Calendario.prototype.imgFlechaDerAnhos='imagenes/cal_flecha_der.gif'
 */